package com.pleague.common.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.pleague.common.reflect.Column;
import com.pleague.common.reflect.Entity;
import com.pleague.common.reflect.ResultSet;

@SuppressWarnings("unchecked")
public class EnvUtil {
	/**
	 * 组装请求报文
	 */
	public static String createEnv(Object object) throws Exception {
		StringBuffer buffer = new StringBuffer("");
		Entity entity = object.getClass().getAnnotation(Entity.class);
		if (entity != null) {
			Field[] fields = object.getClass().getDeclaredFields();
			for (Field field : fields) {
				String fieldName = field.getName();
				String methodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
				Column column = field.getAnnotation(Column.class);
				if (column != null) {
					Method method = object.getClass().getMethod(methodName);
					String value = (String) method.invoke(object);
					if (StringUtils.isEmpty(value)) {
						buffer.append("" + "|");
					} else {
						buffer.append(value + "|");
					}
				} else {
					/* 定义数据集对象 */
					ResultSet resultSet = field.getAnnotation(ResultSet.class);

					if (resultSet != null) {
						Method method = object.getClass().getMethod(methodName);
						Class<?> type = resultSet.type();
						List<?> records = (List<?>) method.invoke(object);

						for (Object record : records) {
							Field[] childfields = type.getDeclaredFields();
							for (Field childField : childfields) {
								String childFieldName = childField.getName();

								String childMethodName = "get" + childFieldName.substring(0, 1).toUpperCase()
										+ childFieldName.substring(1);

								Column childColumn = childField.getAnnotation(Column.class);
								if (childColumn != null) {
									Method childMethod = record.getClass().getMethod(childMethodName);

									String value = (String) childMethod.invoke(record);

									if (StringUtils.isEmpty(value)) {
										buffer.append("" + "&");
									} else {
										buffer.append(value + "&");
									}
								}
							}
							buffer.append(";");
						}
						buffer.append("|");
					}
				}
			}
		}
		return buffer.toString();
	}

	/**
	 * 解析响应报文
	 */
	public static <T> T parseEnv(String responseContent, Class<T> classes) throws Exception {
		String[] responses = responseContent.split("\\|");
		// responses[0] = responses[0].substring(responses[0].length() - 4,
		// responses[0].length());
		// Class<T> classes = Class.forName(object.getName());
		T obj = (T) classes.newInstance();
		int i = 0;
		Field[] fields = classes.getDeclaredFields();
		for (Field field : fields) {
			String fieldName = field.getName();
			String methodName = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
			Column column = field.getAnnotation(Column.class);
			if (column != null) {
				Method method = classes.getMethod(methodName, String.class);
				if (i < responses.length) {
					method.invoke(obj, responses[i]);
					i++;
				}
			} else {
				ResultSet resultSet = field.getAnnotation(ResultSet.class);
				if (resultSet != null) {
					if (i < responses.length) {
						Class<?> type = resultSet.type();
						Method method = classes.getMethod(methodName, List.class);
						List<T> records = new ArrayList<T>();
						if (!StringUtils.isEmpty(responses[i])) {
							/* 数据子集实体以;隔开 */
							String[] childBody = responses[i].split(";");
							for (int m = 0; m < childBody.length; m++) {
								Class<?> childClass = Class.forName(type.getName());
								T child = (T) childClass.newInstance();
								int j = 0;
								/* 数据子集实体属性以&隔开 */
								String[] recordBody = childBody[m].split("&");
								Field[] childFields = type.getDeclaredFields();
								for (Field childField : childFields) {
									String childFieldName = childField.getName();
									String childMethodName = "set" + childFieldName.substring(0, 1).toUpperCase()
											+ childFieldName.substring(1);
									Column childColumn = childField.getAnnotation(Column.class);
									if (childColumn != null) {
										if (j < recordBody.length) {
											Method childMethod = childClass.getMethod(childMethodName, String.class);
											childMethod.invoke(child, recordBody[j]);
											j++;
										}
									}
								}
								records.add(child);
							}
						}
						method.invoke(obj, records);
						i++;
					}
				}
			}
		}
		return obj;
	}

}
